/**
 * Created by XianTao on 2016/11/11
 * <p/>
 * Copyright (c) 2015-2015
 * Apache License
 * Version 2.0, January 2004
 * http://www.apache.org/licenses/
 */
package org.lego.wechat.base;

import org.lego.handle.exception.HandleExceptionApplication;
import org.lego.handle.exception.entity.ExceptionMessage;
import org.lego.handle.exception.service.ExceptionMessageService;
import org.lego.wechat.base.configuration.ConstantMessageKey;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Import;
import org.springframework.util.ObjectUtils;

@SpringBootApplication
@Import(HandleExceptionApplication.class)
public class WechatBaseApplication {
    public static void main(String[] args) {
        SpringApplication.run(WechatBaseApplication.class, args);
    }

    @Bean(name = "initWechatBaseApplication")
    public InitializingBean initializingBean() {
        return new InitializingBean() {
            @Autowired
            private ExceptionMessageService exceptionMessageService;

            public void afterPropertiesSet() throws Exception {
                // 插入初始化数据
                if (ObjectUtils.isEmpty(exceptionMessageService.findOne(ConstantMessageKey.WX_CORP_NEED_PARAM_ID))) {
                    exceptionMessageService.saveAndFlush(new ExceptionMessage(ConstantMessageKey.WX_CORP_NEED_PARAM_ID, "需要id字段"));
                }
                if (ObjectUtils.isEmpty(exceptionMessageService.findOne(ConstantMessageKey.WX_CORP_NOT_FOUND_BY_ID))) {
                    exceptionMessageService.saveAndFlush(new ExceptionMessage(ConstantMessageKey.WX_CORP_NOT_FOUND_BY_ID, "通过WxCorp.id【%s】未找到记录"));
                }
                if (ObjectUtils.isEmpty(exceptionMessageService.findOne(ConstantMessageKey.WX_CORP_NOT_FOUND_BY_CORPID_AND_AGENTID))) {
                    exceptionMessageService.saveAndFlush(new ExceptionMessage(ConstantMessageKey.WX_CORP_NOT_FOUND_BY_CORPID_AND_AGENTID, "通过WxCorp.corpId【%s】和WxCorp.agentId【%s】未找到记录"));
                }
                if (ObjectUtils.isEmpty(exceptionMessageService.findOne(ConstantMessageKey.WX_ACCESS_TOKEN_NEED_PARAM_ID))) {
                    exceptionMessageService.saveAndFlush(new ExceptionMessage(ConstantMessageKey.WX_ACCESS_TOKEN_NEED_PARAM_ID, "需要id字段"));
                }
                if (ObjectUtils.isEmpty(exceptionMessageService.findOne(ConstantMessageKey.WX_ACCESS_TOKEN_NOT_FOUND_BY_ID))) {
                    exceptionMessageService.saveAndFlush(new ExceptionMessage(ConstantMessageKey.WX_ACCESS_TOKEN_NOT_FOUND_BY_ID, "通过WxAccessToken.id【%s】未找到记录"));
                }
                // 插入微信api初始化数据
                if (ObjectUtils.isEmpty(exceptionMessageService.findOne(ConstantMessageKey.WECHAT_SYSTEM_API_ERROR_CODE))) {
                    exceptionMessageService.saveAndFlush(new ExceptionMessage(ConstantMessageKey.WECHAT_SYSTEM_API_ERROR_CODE, "调用微信接口出错【%s】"));
                }
            }
        };
    }
}
